﻿using System;
using System.Collections.Generic;

namespace ECSBase.Component.MaskRelated
{
    public class MaskContainer
    {
        public int None
        {
            get { return 0; }
        }

        private Dictionary<Type, int> MaskDisDictionary = new Dictionary<Type, int>();//存储的字典

        /// <summary>
        /// 分发Mask
        /// </summary>
        /// <param name="t">请求的类型</param>
        /// <returns>对应类型的Mask</returns>
        public int GetMaskOfType(Type t)
        {
            lock (MaskDisDictionary)//避免同时分发一个相同的mask给不同的类，或者同时分发给一个类两个mask
            {
                if (MaskDisDictionary.ContainsKey(t))
                {
                    return MaskDisDictionary[t];
                }

                int newMask = 1 << MaskDisDictionary.Count;
                MaskDisDictionary.Add(t, newMask);

                return newMask;
            }
        }

        /// <summary>
        /// 只有查询功能，无法分发
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public int this[Type t]
        {
            get
            {
                if (MaskDisDictionary.ContainsKey(t))
                {
                    return GetMaskOfType(t);
                }
                return None;
            }
        }

        /// <summary>
        /// 只有查询功能，无法分发
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public int this[string typeName]
        {
            get
            {
                Type t = Type.GetType(typeName);
                return this[t];
            }
        }

        /// <summary>
        /// 获取若干个类的Mask的或，辅助进行筛选
        /// </summary>
        /// <param name="types"></param>
        /// <returns></returns>
        public int GetMaskOfTypes(params Type[] types)
        {
            int _resMask = 0;

            int _typeCount = types.Length;
            for (int i = 0; i < _typeCount; i++)
            {
                _resMask |= this.GetMaskOfType(types[i]);
            }

            return _resMask;
        }
    }
}
